package com.kun.controller;

import com.kun.entity.Dept;
import com.kun.service.impl.DeptServiceImpl;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptServiceImpl deptService;

    @Autowired
    private DiscoveryClient discoveryClient;


    // 根据id查询部门信息
    @GetMapping("/dept/{id}")
    public Object getDept(@PathVariable("id") Long id) {
        return deptService.getDeptById(id);
    }

    // 查询所有部门信息
    @GetMapping("/dept/queryAllList")
    public Object getDepts() {
        return deptService.queryAllDepts();
    }

    // 删除部门信息
    @GetMapping("/dept/delete/{id}")
    public Object deleteDept(@PathVariable("id") Long id) {
        return deptService.deleteDept(id);
    }

    // 插入部门信息
    @PostMapping("/dept/add")
    public Object addDept(@RequestBody Dept dept) {
        return deptService.insertDept(dept);
    }

    // 更新部门信息
    @PostMapping("/dept/update")
    public Object updateDept(@RequestBody Dept dept) {
        return deptService.updateDept(dept);
    }

    //获得联合信息
    @GetMapping("/dept/getServicesInfo")
    public Object getServicesInfo() {
        //1. 获得所有服务信息
        List<String> services = discoveryClient.getServices();
        services.forEach(service -> System.out.println(service));
        //2. 获得指定服务的信息（通过服务名获取）
        List<ServiceInstance> instances = discoveryClient.getInstances("springcloud-provider-dept");
        System.out.println("springcloud-provider-dept:8001 服务信息如下："+instances.size());
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId() + "\t"
                    + instance.getHost() + "\t"
                    + instance.getPort() + "\t"
                    + instance.getUri());
        }
        return "success";
    }
}
